#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cstring>
#include "cJSON.h"
#define BUFFSIZE 1024

char* to1(){
    cJSON *mjson=cJSON_CreateObject();
    cJSON_AddNumberToObject(mjson,"code",1);
    cJSON_AddStringToObject(mjson,"name","root");
    cJSON_AddStringToObject(mjson,"password","123456");
    char *buf= cJSON_Print(mjson);
    return buf;
}

void from1(char *token,char *buf){
    cJSON *msjon= cJSON_Parse(buf);
    cJSON *cjson_token= cJSON_GetObjectItem(msjon,"token");
    token=cjson_token->valuestring;
}

char* to2(char *token){
    cJSON *msjon=cJSON_CreateObject();
    cJSON_AddNumberToObject(msjon,"code",3);
    cJSON_AddStringToObject(msjon,"token",token);
    char *buf= cJSON_Print(msjon);
    return buf;
}

char* to3(char *token){
    cJSON *msjon=cJSON_CreateObject();
    cJSON_AddNumberToObject(msjon,"code",5);
    cJSON_AddStringToObject(msjon,"token",token);
    char *buf= cJSON_Print(msjon);
    return buf;
}

char* to4(char* token){
    cJSON *msjon=cJSON_CreateObject();
    cJSON_AddNumberToObject(msjon,"code",7);
    cJSON_AddStringToObject(msjon,"token",token);
    cJSON_AddNumberToObject(msjon,"station",1);
    char *buf= cJSON_Print(msjon);
    return buf;
}

char* to5(char *token){
    cJSON *msjon=cJSON_CreateObject();
    cJSON_AddNumberToObject(msjon,"code",9);
    cJSON_AddStringToObject(msjon,"token",token);
    cJSON_AddStringToObject(msjon,"start","2022-5-17");
    cJSON_AddStringToObject(msjon,"end","2022-50-19");
    char *buf= cJSON_Print(msjon);
    return buf;
}

int main(int argc, char *argv[])
{
    char *token=0;
    int client_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    int sin_size = 0;
    char buf[BUFFSIZE] = {0};
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.141.128");
    server_addr.sin_port = htons(8000);

    if((client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        return -1;
    }
    socklen_t sin_size_;
    sin_size_= sizeof(struct sockaddr_in);
//    while(1)
//    {
//        scanf("%s",buf);
//        if(!strcmp(buf,"quit"))
//        {
//            break;
//        }
//
//        if((len = sendto(client_sockfd, buf, strlen(buf), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
//        {
//            perror("sendto fail!\n");
//            return -1;
//        }
//    }
    char *buf_test=0;
    //放置to函数......
    buf_test=to1();
    std::cout<<"发送to1："<<buf<<std::endl;
    if((len = sendto(client_sockfd, buf_test, strlen(buf_test), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return -1;
    }
    sin_size= sizeof(struct sockaddr_in);
    if((len = recvfrom(client_sockfd, buf_test, BUFFSIZE, 0, (struct sockaddr*)&server_addr,
                       &sin_size_)) < 0)
    {
        perror("recvfrom error!\n");
        return -1;
    }
    //放置from函数......
    from1(token,buf_test);
    std::cout<<"接收from1："<<buf<<std::endl;
    //............................................................
    std::cout<<"发送to2："<<buf<<std::endl;
    if((len = sendto(client_sockfd, buf_test, strlen(buf_test), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return -1;
    }
    sin_size= sizeof(struct sockaddr_in);
    if((len = recvfrom(client_sockfd, buf_test, BUFFSIZE, 0, (struct sockaddr*)&server_addr,
                       &sin_size_)) < 0)
    {
        perror("recvfrom error!\n");
        return -1;
    }
    //放置from函数......
    std::cout<<"接收from2："<<buf<<std::endl;
    //............................................................
    std::cout<<"发送to3："<<buf<<std::endl;
    if((len = sendto(client_sockfd, buf_test, strlen(buf_test), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return -1;
    }
    sin_size= sizeof(struct sockaddr_in);
    if((len = recvfrom(client_sockfd, buf_test, BUFFSIZE, 0, (struct sockaddr*)&server_addr,
                       &sin_size_)) < 0)
    {
        perror("recvfrom error!\n");
        return -1;
    }
    //放置from函数......
    std::cout<<"接收from3："<<buf<<std::endl;
    //............................................................
    //............................................................
    std::cout<<"发送to4："<<buf<<std::endl;
    if((len = sendto(client_sockfd, buf_test, strlen(buf_test), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return -1;
    }
    sin_size= sizeof(struct sockaddr_in);
    if((len = recvfrom(client_sockfd, buf_test, BUFFSIZE, 0, (struct sockaddr*)&server_addr,
                       &sin_size_)) < 0)
    {
        perror("recvfrom error!\n");
        return -1;
    }
    //放置from函数......
    std::cout<<"接收from4："<<buf<<std::endl;
    //............................................................
    //............................................................
    std::cout<<"发送to5："<<buf<<std::endl;
    if((len = sendto(client_sockfd, buf_test, strlen(buf_test), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return -1;
    }
    sin_size= sizeof(struct sockaddr_in);
    if((len = recvfrom(client_sockfd, buf_test, BUFFSIZE, 0, (struct sockaddr*)&server_addr,
                       &sin_size_)) < 0)
    {
        perror("recvfrom error!\n");
        return -1;
    }
    //放置from函数......
    std::cout<<"接收from5："<<buf<<std::endl;

    close(client_sockfd);
    return 0;
}